package com.superman.util;
import com.superman.entity.Record_User_Car;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class JPanelRecordAdmin {
    private JTable paramTable;
    private static final String[] paramColumnName = {"编号", "汽车名称", "备注", "品牌", "类型", "价格", "租金总额", "借车时间", "还车时间", "用户"};

    public JPanel getJPanelRecord(List<Record_User_Car> objectList){

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(5, 5));
        centerPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        //表头
        paramTable = createParamTable();

        // 创建包含表格的滚动窗格
        JScrollPane paramScrollPane = new JScrollPane(paramTable);
        centerPanel.add("Center", paramScrollPane);
        //设置表格宽度
        paramTable.getColumnModel().getColumn(4).setPreferredWidth(30);


        Object[][] paramRowData = null;
        if (objectList.isEmpty()) {
            paramRowData = new String[1][paramColumnName.length];
        } else {
            paramRowData = new Object[objectList.size()][paramColumnName.length];
            for (int i = 0; i < objectList.size(); i++) {
                Record_User_Car ruc = objectList.get(i);
                paramRowData[i][0] = i + 1;
                paramRowData[i][1] = ruc.getModel();
                paramRowData[i][2] = ruc.getT_comments();
                paramRowData[i][3] = ruc.getBrand();
                paramRowData[i][4] = ruc.getCategory();
                paramRowData[i][5] = ruc.getRent();
                paramRowData[i][6] = ruc.getPayment();
                paramRowData[i][7] = ruc.getStart_date();
                paramRowData[i][8] = ruc.getReturn_date();
                paramRowData[i][9] = ruc.getUsername();
            }
        }

        paramTable.setModel(new DefaultTableModel(paramRowData, paramColumnName) );
        paramTable.setRowHeight(20);//设置行高
        return centerPanel;
    }

    /**
     * 参数列表初始化
     *
     * @return
     */
    private JTable createParamTable() {
        String[][] rowData = new String[1][paramColumnName.length];
        return new JTable(new DefaultTableModel(rowData, paramColumnName));
    }
}
